home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / str / strhead.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  1.4 KB  |  63 lines

  1. #include <stdio.h>
  2. main(argc,argv)
  3. int argc;
  4. char *argv[];
  5. {
  6. char c,s[256],z[256],e[256];
  7. int i,n,o;
  8.  
  9. if(argc==1) { /* without parameter return instructions */
  10.    printf("strhead returns the first part of a string, up to a terminator\n");
  11.    printf("specified as second parameter\n");
  12.    printf("strhead abcde.fgh .fg\n will produce \n abcde \n");
  13.    printf("you may specify an offset as optional 3rd parameter\n");
  14.    printf("\n(C) Rainer Kowallik\n");
  15. }
  16.  
  17.    o=0;
  18.    if(argc==4) o=atoi(argv[3]);
  19.  
  20.    strcpy(s,argv[1]);      /* source string */
  21.    strcpy(z,argv[2]);      /* terminator string */
  22.    i=instr(z,s);
  23.    strcpy(e,s);
  24.    if(i>0) midstr(e,s,0,i-1+o);
  25.    printf("%s\n",e);
  26.    exit(0);
  27. }
  28.  
  29. /* -------------------------------------------
  30.    return position of a substring in a string
  31.    ------------------------------------------- */
  32. instr(substr,str)
  33. char  str[],substr[];
  34. {  short i,p,flg,l1,l2;
  35.  
  36.    l1=strlen(str); l2=strlen(substr);
  37.    for(p=0; p < l1; p++) {
  38.       flg=0;
  39.       for(i=0; i < l2; i++) {
  40.          if(str[p+i] != substr[i]) {
  41.             flg = -1; break;
  42.          }
  43.       }
  44.       if(flg == 0) return(p);
  45.    }
  46.    return(-1);
  47. }
  48.  
  49.  
  50. /* -------------------------------------------------
  51.    return a substring from within a mainstring
  52.    ------------------------------------------------- */
  53. midstr(substr,str,ss,es)
  54. char  substr[],str[];
  55. short ss,es;
  56. {  short i,j;
  57.  
  58.    i=0;
  59.    for(j=ss; j <= es; j++) substr[i++]=str[j];
  60.    substr[i]=0;
  61. }
  62.  
  63.